package com.hnichr.ump.ecard.service.impl;

import com.hnichr.ump.common.ecard.entity.EcardBusinessCard;
import com.hnichr.ump.common.ecard.entity.EcardCompany;
import com.hnichr.ump.common.ecard.service.EcardCompanyService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class EcardCompanyServiceImpl extends BaseServiceImpl<EcardCompany> implements EcardCompanyService {

    @Transactional
    @Override
    public String remove(String[] ids){
        if(ArrayUtils.isEmpty(ids)){
            return null;
        }

        List<String> idList = Arrays.asList(ids);
        List<EcardBusinessCard> cardList =  JpaUtil.linq(EcardBusinessCard.class).in("companyId",idList).list();
        if(CollectionUtils.isNotEmpty(cardList)){
            EcardCompany company = this.getById(EcardCompany.class,cardList.get(0).getCompanyId());
            return company.getCompanyName()+"正在使用，不能删除";
        }
        JpaUtil.lind(EcardCompany.class).in("id",idList).delete();
        return null;
    }
}
